package org.thss.petri.gef.actions;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.ui.actions.EditorPartAction;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.thss.petri.gef.editparts.PetriNetEditPart;
import org.thss.petri.gef.ui.PetriNetEditor;

public class FirePetriNetAction extends EditorPartAction {

	private IEditorPart editor;
	
	
	public FirePetriNetAction(IEditorPart editor) {
		super(editor);
		this.editor = editor;
		setLazyEnablementCalculation(false);
	}

	@Override
	protected void init() {
		super.init();
		setText("Fire Petri Net");
		setId("fire_petrinet_action");
		setToolTipText("Fire the petri net");
		ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
		setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_UP));
		setDisabledImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_UP_DISABLED));
		setEnabled(false);
	}
	
	@Override
	protected boolean calculateEnabled() {
		Command cmd =  createFirePetriNetCommand();
		
		if(cmd == null){
			return false;
		}
		
		return cmd.canExecute();
	}
	
	public Command createFirePetriNetCommand(){
		PetriNetEditor editor = (PetriNetEditor)this.editor;
		EditPart netEditPart = editor.getPetriNetEditPart();
		Request request = new Request("fire_petri_net_action");
		
		if(netEditPart == null || !( netEditPart instanceof PetriNetEditPart)){
			return null;
		}
	
		Command cmd = netEditPart.getCommand(request);
		return cmd;
	}

	@Override
	public void run() {
		execute(createFirePetriNetCommand());
	}
	
}
